/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * HeroStats.java
 *
 * Created on 02-jun-2011, 15:19:48
 */
package herostats;

import javax.swing.DefaultComboBoxModel;
import libs.Extends.tableRenderer;
import javax.swing.UIManager;
import javax.swing.UIManager.LookAndFeelInfo;
import javax.swing.UnsupportedLookAndFeelException;
import libs.Extends.GameTablemodel;
import libs.Extends.HeroAvgTablemodel;
import libs.Extends.HeroTotalTablemodel;

/**
 *
 * @author PHENOM II
 */
public class HeroStats extends javax.swing.JFrame {

    boolean isOk;
    private GameTablemodel games;
    private HeroAvgTablemodel HAvg;
    private HeroTotalTablemodel HTotal;

    /** Creates new form HeroStats */
    public HeroStats() {
        isOk = loadData();
        if (isOk) {
            initComponents();
            tlbTable.setDefaultRenderer(Object.class, new tableRenderer());
            tlbTable.setDefaultRenderer(Integer.class, new tableRenderer());
            tlbTable.setDefaultRenderer(String.class, new tableRenderer());
            tlbTable.setDefaultRenderer(Double.class, new tableRenderer());
        }
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnlRads = new javax.swing.JPanel();
        cbxMode = new javax.swing.JComboBox();
        btnLoad = new javax.swing.JButton();
        jcpScrool = new javax.swing.JScrollPane();
        tlbTable = new javax.swing.JTable();
        lbl = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Hero stats");

        pnlRads.setBorder(javax.swing.BorderFactory.createTitledBorder("Data type"));

        cbxMode.setModel(mountComboBox());
        cbxMode.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbxModeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnlRadsLayout = new javax.swing.GroupLayout(pnlRads);
        pnlRads.setLayout(pnlRadsLayout);
        pnlRadsLayout.setHorizontalGroup(
            pnlRadsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRadsLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(cbxMode, 0, 200, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnlRadsLayout.setVerticalGroup(
            pnlRadsLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlRadsLayout.createSequentialGroup()
                .addComponent(cbxMode, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        btnLoad.setText("Load data again");
        btnLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoadActionPerformed(evt);
            }
        });

        tlbTable.setAutoCreateRowSorter(true);
        tlbTable.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        tlbTable.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jcpScrool.setViewportView(tlbTable);

        lbl.setText("Date");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jcpScrool, javax.swing.GroupLayout.DEFAULT_SIZE, 989, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(pnlRads, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLoad)
                        .addGap(50, 50, 50)
                        .addComponent(lbl)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(pnlRads, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(25, 25, 25)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnLoad)
                            .addComponent(lbl))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jcpScrool, javax.swing.GroupLayout.DEFAULT_SIZE, 483, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private boolean loadData() {
        Data loader = new Data();
        if (loader.getDate() == null) {
            return false;
        } else {
            games = new GameTablemodel(loader);
            HAvg = new HeroAvgTablemodel(loader);
            HTotal = new HeroTotalTablemodel(loader);
            lbl.setText(loader.getDate());
            cbxMode.setSelectedIndex(0);
            tlbTable.setModel(games);
            return true;
        }

    }
    private void btnLoadActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoadActionPerformed
        loadData();
    }//GEN-LAST:event_btnLoadActionPerformed

    private void cbxModeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbxModeActionPerformed
        switch (cbxMode.getSelectedIndex()) {
            case 0:
                tlbTable.setModel(games);
                break;
            case 1:
                tlbTable.setModel(HTotal);
                break;
            case 2:
                tlbTable.setModel(HAvg);
                break;
            default:
                System.out.println("Default selected on cbx");
                tlbTable.setModel(games);
                break;
        }
    }//GEN-LAST:event_cbxModeActionPerformed

    private DefaultComboBoxModel mountComboBox() {
        String l[] = {"Games View", "Total heros view", "Average Heros view"};
        return new DefaultComboBoxModel(l);
    }

    public static void main(String args[]) {
        changeLookAndFeel();
        try {
            java.awt.EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    HeroStats h = new HeroStats();
                    if (h.isOk) {
                        h.setVisible(true);
                    }
                }
            });
        } catch (java.lang.ExceptionInInitializerError e) {
            System.out.println(e.getCause().getMessage());
            System.out.println(e.getException().getMessage());
            System.out.println("IF you are running on windows, please do the next in order to fix this problem: ");
            System.out.println("Hold the windows button of your keyboard and pres the R key and a little window will appear.");
            System.out.println("Type this: C:\\Windows\\System32\\cttune.exe and press enter, tick the square, follow the indications and run this awesome program again");
        }
    }

    private static void changeLookAndFeel() {
        try {
            for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    UIManager.setLookAndFeel(info.getClassName());
                    return;
                }
            }
        } catch (UnsupportedLookAndFeelException e) {
            // handle exception
        } catch (ClassNotFoundException e) {
            // handle exception
        } catch (InstantiationException e) {
            // handle exception
        } catch (IllegalAccessException e) {
            // handle exception
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLoad;
    private javax.swing.JComboBox cbxMode;
    private javax.swing.JScrollPane jcpScrool;
    private javax.swing.JLabel lbl;
    private javax.swing.JPanel pnlRads;
    private javax.swing.JTable tlbTable;
    // End of variables declaration//GEN-END:variables
}
